same_group_spec = list(
  "public" = c("public_non_observer", "public_observer"),
  "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
)


coef_label <- function(x) {
  x %>%
    str_replace_all("pair_includes_trans_alt", "Worker is trans") %>%
    str_replace_all("n3", "Predicted prob. of others choosing trans (community)") %>%
    str_replace_all("group_predic_choose_trans", "Predicted prob. of others choosing trans (group)") %>%
    str_replace_all("control_high_n_discuss_(200|500|1000)", "No discussion (private) + Above med. number of discussion groups nearby") %>%
    str_replace_all("control_high_p_discuss_(200|500|1000)", "No discussion (private) + Above med. proportion of discussion groups nearby") %>%
    str_replace_all("attitude_val", "Disagreed with discrimination (=1)") %>%
    str_replace_all("likelyTRUE", "Trans likely to complete delivery (=1)") %>%
    str_replace_all("video_typeplacebo", "Rights messaging video") %>%
    str_replace_all("dominates", "Worker dominates") %>%
    str_replace_all("dominated", "Worker is dominated") %>%
    str_replace_all("discussion_pooled", "Discussion (pooled)") %>%
    str_replace_all("treat_type_r2_pooled", "") %>%
    str_replace_all("p_other_selected_trans\\:is_listener", "$\\\\pi_{-i}$ $\\\\times$ Listened to 2-person discussion") %>%
    str_replace_all("p_other_selected_trans\\:is_listener", "$\\\\pi_{-i}$ $\\\\times$ Listened to 2-person discussion") %>%
    str_replace_all("p_other_selected_trans\\:public_non_observer", "$\\\\pi_{-i}$ $\\\\times$ Non-observer (No discussion, public)") %>%
    str_replace_all("p_other_selected_trans\\:", "$\\\\pi_{-i}$ $\\\\times$ ") %>%
    str_replace_all("public_observer", "Observer (No discussion, public)") %>%
    str_replace_all("public_non_observer", "Non-observer (No discussion, public)") %>%
    str_replace_all("observer($|\\:)", "Observer") %>%
    str_replace_all("discussion_pair_speaker", "Speaker (2-person discussion)") %>%
    str_replace_all("discussion_pair_listener", "Listener (2-person discussion)") %>%
    str_replace_all("public$", "No discussion (public)") %>%
    str_replace_all("control", "No discussion (private)") %>%
    str_replace_all("discussant", "Discussion participant") %>%
    str_replace_all("pair_includes_trans_discussion_full", "Worker is trans $\\\\times$ 3-person discussion") %>%
    str_replace_all("video_typetreatment", "Legal rights video") %>%
    str_replace_all("trans_in_list_group_group_labeldiscuss", fixed("Anti-trans statement in list $\\\\times$ 3-person discussion")) %>%
    str_replace_all("group_label($|discuss)", "3-person discussion") %>%
    str_replace_all("pair_includes_transTRUE", "Worker is trans") %>%
    str_replace_all("video_type_labelRights Messaging", "Rights messaging video") %>%
    str_replace_all("video_type_labelLegal Rights", "Legal rights video") %>%
    str_replace_all("pair_includes_trans", "Worker is trans") %>%
    str_replace_all("\\:", " $\\\\times$ ") %>%
    str_replace_all("pair_includes_femaleTRUE", "Worker is female") %>%
    str_replace_all("r2_reliability_diff", "Relative reliability score") %>%
    str_replace_all("r2_reliability_shown(TRUE)?", "Reliability score is shown (=1)") %>%
    str_replace_all("discussion_pair", "2-person discussion") %>%
    str_replace_all("delivery_incentive_expmulti", "Multiple deliveries") %>%
    str_replace_all("delivery_incentive_exp(Missing)", "Before multiple deliveries randomisation") %>%
    str_replace_all("item_diff_value_100", "Relative value of items offered (Rs. / 100)") %>%
    str_replace_all("item_diff", "Relative \\\\# items offered") %>%
    str_replace_all("duration_choice_winsorised|duration_r2_choice_winsorised", "Duration (mins)") %>%
    str_replace_all("quality_included", "Reliability score is shown (=1)") %>%
    str_replace_all("quality_diff", "Relative reliability score") %>%
    str_replace_all("trans_in_list_group", "Anti-trans statement in list") %>%
    str_replace_all("list_b", "List B (=1)") %>%
    str_replace_all("trans_photo_ynTRUE", "Photo is trans") %>%
    str_replace_all("trans_photo_yn", "Photo is trans") %>%
    # str_replace_all("n_salience_minus_trans", "\\\\# Other statements remembered") %>%
    str_replace_all("prop_salience_minus_trans", "Proportion of non-trans words remembered") %>%
    str_replace_all("purpose_0_transTRUE", "Correctly guessed purpose (after main outcome)") %>%
    str_replace_all("purpose_0_trans", "Correctly guessed purpose (after main outcome)") %>%
    str_replace_all("delivery_incentive_multi", "3 deliveries") %>%
    str_replace_all("purpose_2_transTRUE", "Correctly guessed purpose (end of experiment)") %>%
    str_replace_all("purpose_2_trans", "Correctly guessed purpose (end of experiment)") %>%
    str_replace_all("high_sdbTRUE", "Above median SDB score") %>%
    str_replace_all("high_sdb_(.+)", "Above median SDB score") %>%
    str_replace_all("high_sdb", "Above median SDB score") %>%
    str_replace_all("discussion_full", "3-person discussion") %>%
    str_replace_all("discuss_type_label3-person discussion", "3-person discussion") %>%
    str_replace_all("discuss_type_labelNo discussion \\(public\\)", "No discussion (public)") %>%
    str_replace_all("discuss_type_label2\\-person discussion", "2-person discussion") %>%
    str_replace_all("3-person discussionTRUE", "3-person discussion") %>%
    str_replace_all("above_median_prop_salience", "Above median proportion of non-trans words remembered") %>%
    str_replace_all("trans_remembered", "Transgender word remembered") %>%
    str_replace_all("video_type_placebo", "Rights messaging video") %>%
    str_replace_all("video_type_treatment", "Legal rights video") %>%
    str_replace_all("treat_type_r2_label", "") %>%
    str_replace_all("gobs9_z_group_excl", "Others in group: observed attitude towards transgenders in discussion (Z-score)") %>%
    str_replace_all("gobs9_z", "Self: observed attitude towards transgenders in discussion (Z-score)") %>%
    str_replace_all("gobs9_z_group_excl", "Others in group: observed attitude towards transgenders in discussion (Z-score)") %>%
    str_replace_all("spoke_pro_trans_group_excl", "P(others in group spoke pro-trans in discussion)") %>%
    str_replace_all("spoke_pro_trans", "Self: number of times spoke in favour of trans in discussion") %>%
    str_replace_all("favourable_discussion_z_group_excl", "Others in group: combined favourable discussion index (Z-score)") %>%
    str_replace_all("favourable_discussion_z", "Self: combined favourable discussion index (Z-score)") %>%
    str_replace_all("is_listenerTRUE", "Listened to 2-person discussion") %>%
    str_replace_all("is_listener", "Listened to 2-person discussion") %>%
    str_replace_all("close_knit_ind_others", "Close relations with others in group") %>%
    str_replace_all("higher_ls_score_others", "High persuasiveness score for discussants") %>%
    str_replace_all("female", "Female (=1)") %>%
    str_replace_all("bachelors", "Bachelor's degree (=1)") %>%
    str_replace_all("married", "Married (=1)") %>%
    str_replace_all("^age$", "Age") %>% 
    str_replace_all("employed", "Employed (=1)") %>%
    str_replace_all("landlord", "Landlord (=1)") %>%
    str_replace_all("children", "Has children (=1)") %>%
    str_replace_all("employer", "Employer (=1)") %>%
    str_replace_all("hindu", "Hindu (=1)") %>%
    str_replace_all("public_pooled", "No discussion (public), pooled") %>%
    str_replace_all("publicTRUE", "No discussion (public)") %>%
    str_replace_all("discussantTRUE", "Discussion participant") %>%
    str_replace_all("hh_size_above_med", "Above med. hh size (=1)") %>%
    str_replace_all("hh_food_exp_pc_above_med", "Above med. hh food exp. p.c. (=1)") %>%
    str_replace_all("group_obs_trans_includedTRUE", "Choice includes a transgender worker (=1)") %>%
    str_replace_all("p_other_selected_trans", "$\\\\pi_{-i}$ = P(others in group selected trans in treatment round)") %>%
    str_replace_all("p_self_selected_trans", "P(selected trans in treatment round)") %>%
    str_replace_all("reason_heard_r1TRUE", "Reason was cited in discussion (=1)") %>%
    str_replace_all("reason_heard_r1_transTRUE", "Reason was cited in discussion for transgender choice (=1)") %>%
    str_replace_all("treat_type_r2choices_observer", "No discussion, public, observer (=1)") %>%
    str_replace_all("audio_refusedTRUE", "Audio recording refused (=1)") %>%
    str_replace_all("treat_type_r1_label", "") %>%
    str_replace_all("treat_type_r1_pooled", "") %>%
    str_replace_all("speaks_english", "Speaks English (=1)") %>%
    str_replace_all("reads_english", "Reads English (=1)") %>%
    str_replace_all("overall_dominance_trans", "Dominance index - transgender choices only (Z)") %>%
    str_replace_all("overall_dominance", "Dominance index (Z)") %>%
    str_replace_all("p_spoke_first_trans", "P(spoke first) - transgender choices only") %>%
    str_replace_all("p_spoke_first", "P(spoke first)") %>%
    str_replace_all("p_dominant_trans", "P(dominated conversation) - transgender choices only") %>%
    str_replace_all("p_dominant", "P(dominated conversation)") %>%
    str_replace_all("photo_genderT", "Photo is trans (=1)") %>%
    str_replace_all("photo_genderW", "Photo is female (=1)") %>%
    str_replace_all("rich_z_diff", "Diff. in perc. wealth (Z)") %>%
    str_replace_all("age_z_diff", "Diff. in perc. age (Z)") %>%
    str_replace_all("lower_caste_z_diff", "Diff. in perc. Scheduled Caste (Z)") %>%
    str_replace_all("educated_z_diff", "Diff. in perc. educated (Z)") %>%
    str_replace_all("neatly_dressed_z_diff", "Diff. in perc. neatly dressed (Z)") %>%
    str_replace_all("comfortable_talking_z_diff", "Diff. in comfort talking (Z)") %>%
    str_replace_all("unsafe_in_home_z_diff", "Diff. in feeling unsafe at home (Z)") %>%
    str_replace_all("worried_talk_family_z_diff", "Diff. in worried about talking to family (Z)") %>%
    str_replace_all("spouse_unhappy_talk_z_diff", "Diff. in spouse unhappy if talking (Z)") %>%
    str_replace_all("discussion_listener", "2-person discussion (listener)") %>%
    str_replace_all("discussion_speaker", "2-person discussion (speaker)") %>%
    str_replace_all("k4_illegal", "Not employing is illegal (=1)") %>%
    str_replace_all("n_r1_choose_trans", "Num. times group chose trans in treatment round") %>%
    str_replace_all("morality_rating_trans", "Discussion morality score, trans choices (Z)") %>%
    str_replace_all("morality_rating_non_trans", "Discussion morality score, non-trans choices (Z)") %>% 
    str_replace_all("discussion_length_words_lead_trans", "\\\\# words from facilitator, trans choices (Z)") %>%
    str_replace_all("discussion_length_words_lead_non_trans", "\\\\# words from facilitator, non-trans choices (Z)") %>%
    str_replace_all("discussion_length_words_trans", "\\\\# words from resp., trans choices (Z)") %>%
    str_replace_all("discussion_length_words_non_trans", "\\\\# words from resp., non-trans choices (Z)") %>%
    str_replace_all("discussion_ratio_lead_trans", "Prop. words from facilitator, trans choices") %>%
    str_replace_all("discussion_ratio_lead_non_trans", "Prop. words from facilitator, non-trans choices")
}

fe_label <- tribble(
  ~raw, ~clean, ~fmt,
  "nobs", "Num. observations", 0,
  "N participants", "Num. participants", 0,
  "N groups", "Num. groups", 0,
  "Control mean", "Outcome mean, control group", 2,
  "Mean: No discussion (private)", "Outcome mean: No discuss. (priv.)", 2,
  "Mean: Control video", "Outcome mean: Control vid.", 2,
  "Mean: Choice doesn't include trans", "Outcome mean: Choice doesn't include trans", 2,
  "Mean: 3-person discussion", "Outcome mean: 3-person discuss.", 2,
  "Mean: 2-person discussion", "Outcome mean: 2-person discuss.", 2,
  "Mean: No discussion (private) + worker is non-trans", "Outcome mean$^\\dagger$: no discuss (priv.), worker non-trans", 2,
  "Mean: No discussion (private) + worker is trans", "Outcome mean$^\\dagger$: no discuss (priv.), worker trans", 2,
  "Mean: No discussion (private) + worker is T", "Outcome mean: no discuss (priv.), worker trans", 2,
  "Mean: Control video + worker is non-trans", "Outcome mean: control vid., worker non-trans", 2,
  "Mean: Control video + worker is trans", "Outcome mean: control vid., worker trans", 2,
  "Mean: Control video + worker is T", "Outcome mean: control vid., worker trans", 2,
  "Mean: No discussion (private) + photo is male", "Outcome mean: no discuss (priv.), male photo", 2,
  "Mean: No discussion (private) + photo is female", "Outcome mean: no discuss (priv.), female photo", 2,
  "Mean: No discussion (private) + photo is trans", "Outcome mean: no discuss (priv.), trans photo", 2,
  "FE: stratum_id", "Stratum controls", 0,
  "LASSO controls", "LASSO controls", 0,
  "FE: video_type", "Video controls", 0,
  "FE: reason_val", "Reason-type FEs", 0,
  "FE: phase", "Phase FEs", 0,
  "FE: delivery_incentive_exp", "Multiple delivery control", 0,
  "FE: comparator_order_in_pair", "Shown on right dummy (=1)", 0,
  "FE: round", "Round FEs", 0,
  "FE: list_b", "Question FEs", 0,
  "FE: ind_id", "Participant FEs", 0,
  "FE: discuss_type", "Discussion arm controls", 0,
  "FE: treat_type_r2_label", "Discussion arm controls", 0,
  "FE: attitude_type", "Question FEs", 0,
  "FE: likelihood_order", "Question FEs", 0
)

fe_label_no_fe <- fe_label %>%
  filter(!str_detect(raw, "FE:|LASSO controls"))


demo_vars <- c("age", "female", "speaks_english", "reads_english", "hindu", "bachelors", "married", "employed", "landlord", "children", "employer", "hh_size", "hh_food_exp_pc") %>%
  set_names(
    c("Age", "Female (=1)", "Speaks English (=1)", "Reads English (=1)", "Hindu (=1)", "Bachelor's degree (=1)", "Married (=1)", "Employed (=1)", "Landlord (=1)", "Num. children", "Employer (=1)", "Household size", "Monthly h.h. food expenditure per capita (Rs.)")
  )

group_relation_vars <- c("any_family", "neighbour", "friend") %>%
  set_names(
    c("Num. family members in group-of-3", "Num. neighbours in group-of-3", "Num. friends in group-of-3")
  )



demo_vars_w_group_relations <- c(demo_vars, group_relation_vars)

# demo_vars_w_group_relations <- c(demo_vars, group_relation_vars)

# demo_vars <- c(demo_vars, group_relation_vars)

extra_vars <- c(
  # "Phase 2 (=1)" = "phase_2",
  "Taken part in market research survey (=1)" = "b11",
  "Has received free item as promotion (=1)" = "b12",
  "Someone in household ordered taxi with app (=1)" = "i1",
  "Someone in household ordered food with app (=1)" = "i2",
  "Someone in household ordered other items with app (=1)" = "i3",
  "Self-reported WTP for delivery" = "dq12",
  "Respondent would normally be household member who receives delivery (=1)" = "normally_receives_delivery",
  "Relative number of items offered by worker" = "item_diff",
  "Relative reliability score" = "r2_reliability_diff",
  "Reliability score is shown (=1)" = "r2_reliability_shown",
  "Reliability score of the benchmark worker" = "r2_reliability_benchmark"
)

control_vars_with_names <- c(
  demo_vars,
  group_relation_vars,
  extra_vars
)

control_vars <- control_vars_with_names %>% unname()

control_vars_except_items_reliability <- str_subset(control_vars, "r2_reliability|item", negate = TRUE)


colour_green <- RColorBrewer::brewer.pal(3, "Set2")[[1]]
colour_orange <- RColorBrewer::brewer.pal(3, "Set2")[[2]]


lasso_options_vids_discuss <-  list(
  potential_controls = control_vars,
  t = c("discussion_full", "video_type_placebo", "video_type_treatment", "discussion_pooled", "public"),
  interact = NULL,
  group_control = TRUE
)

lasso_options_no_interact <- list(
  potential_controls = control_vars,
  t = c("discussion_full", "video_type_control", "video_type_placebo", "video_type_treatment", "discussion_pooled", "public"),
  interact = NULL,
  group_control = TRUE
)


# SPECS FOR RI
ri <- TRUE
ri_sims <- 1000

ri_sims %>% write_stat("outputs/stats/ri_sims.tex", digits = 0)

var_types_spec <- list(
  "pair_includes_trans" = "within",
  "pair_includes_trans_alt" = "within",
  "control" = "between",
  "discussant" = "between",
  "public" = "between",
  "public_pooled" = "between",
  "group_label" = "between",
  "video_type_control" = "between",
  "video_type_placebo" = "between",
  "video_type_treatment" = "between",
  "item_diff" = "across",
  "discussion_full" = "between",
  "public_observer" = "ind",
  "public_non_observer" = "ind",
  "discussion_pair_speaker" = "ind",
  "discussion_pair_listener" = "ind",
  "discussion_full" = "between",
  "is_listener" = "ind",
  "control" = "between",
  "p_other_selected_trans" = "non_random",
  "prop_salience_minus_trans" = "non_random",
  "group_predic_member_is_speaker" = "ind",
  "group_predic_member_is_speaker" = "ind",
  "trans_in_list_group" = "within",
  "trans_photo_yn" = "within"
)


control_rows <-    list(
  "Controls" = c("", "X", "X"),
  "Controls interacted with worker is trans" = c("", "X", "")
)